#!/usr/bin/env bash
# Copyright (c) 2024 The Toltec Contributors
# SPDX-License-Identifier: MIT

archs=(rm1 rm2)
pkgnames=(oxide oxide-extra oxide-utils inject_evdev liboxide liboxide-dev libsentry)
_oxidever=2.8.4
pkgver=$_oxidever-2
_sentryver=0.7.6
timestamp=2024-06-26T22:31:46Z
maintainer="Eeems <eeems@eeems.email>"
url=https://oxide.eeems.codes
license=MIT
flags=(patch_rm2fb)
image=qt:v3.1
source=(
    "https://github.com/Eeems-Org/oxide/archive/refs/tags/v$_oxidever.zip"
    toltec-rm2-override.conf
    launcherctl-oxide
)
sha256sums=(
    bfcb26c0493544fabc472bebc1eb372e49e58774163533ae937b6e814222f03e
    SKIP
    SKIP
)

build() {
    find . -name "*.pro" -type f -print0 \
        | xargs -r -0 sed -i 's/linux-oe-g++/linux-arm-remarkable-g++/g'
    find . -name "*.pri" -type f -print0 \
        | xargs -r -0 sed -i 's/linux-oe-g++/linux-arm-remarkable-g++/g'
    CMAKE_TOOLCHAIN_FILE="/usr/share/cmake/$CHOST.cmake" make FEATURES=sentry release
}

oxide() {
    pkgdesc="Launcher application"
    section="launchers"
    installdepends=("oxide-utils=$pkgver" "liboxide=$pkgver" "libsentry=$_sentryver" reboot-guard jq display launcherctl)
    replaces=(erode tarnish decay corrupt)
    conflicts=(erode tarnish decay corrupt)

    package() {
        # System service
        install -D -m 644 -t "$pkgdir"/etc/dbus-1/system.d "$srcdir"/release/etc/dbus-1/system.d/codes.eeems.oxide.conf
        install -D -m 644 -t "$pkgdir"/lib/systemd/system "$srcdir"/release/etc/systemd/system/tarnish.service
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/tarnish
        # Task manager
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/erode
        install -D -m 644 -t "$pkgdir"/opt/usr/share/icons/oxide/48x48/apps "$srcdir"/release/opt/usr/share/icons/oxide/48x48/apps/erode.png
        install -D -m 644 -t "$pkgdir"/opt/usr/share/icons/oxide/702x702/splash "$srcdir"/release/opt/usr/share/icons/oxide/702x702/splash/erode.png
        install -D -m 644 -t "$pkgdir"/opt/usr/share/applications "$srcdir"/release/opt/usr/share/applications/codes.eeems.erode.oxide
        # Launcher
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/oxide
        install -D -m 644 -t "$pkgdir"/opt/etc "$srcdir"/release/opt/etc/oxide.conf
        install -D -m 644 -t "$pkgdir"/opt/usr/share/applications "$srcdir"/release/opt/usr/share/applications/codes.eeems.oxide.oxide
        install -D -m 644 -t "$pkgdir"/opt/usr/share/icons/oxide/702x702/splash "$srcdir"/release/opt/usr/share/icons/oxide/702x702/splash/oxide.png
        # Lockscreen
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/decay
        install -D -m 644 -t "$pkgdir"/opt/usr/share/applications "$srcdir"/release/opt/usr/share/applications/codes.eeems.decay.oxide
        # Task switcher
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/corrupt
        install -D -m 644 -t "$pkgdir"/opt/usr/share/applications "$srcdir"/release/opt/usr/share/applications/codes.eeems.corrupt.oxide
        # launcherctl registration
        install -D -T -m 755 "$srcdir"/launcherctl-oxide "$pkgdir"/opt/share/launcherctl/oxide
        if [[ $arch = rm2 ]]; then
            install -D -m 644 -t "$pkgdir"/etc/systemd/system/tarnish.service.d \
                "$srcdir"/toltec-rm2-override.conf
        fi
    }

    configure() {
        systemctl daemon-reload

        if ! launcherctl is-current-launcher oxide; then
            echo ""
            echo "Run the following command(s) to use oxide as your launcher"
            echo "launcherctl switch-launcher --start oxide"
            echo ""
        fi
    }

    preremove() {
        # Just in case more than one launcher is active, do individual checks
        if launcherctl is-active-launcher oxide || launcherctl is-enabled-launcher oxide; then
            launcherctl switch-launcher --start xochitl
        fi
    }

    postremove() {
        systemctl daemon-reload
    }
}

oxide-extra() {
    pkgdesc="Extra applications for oxide"
    section="utils"
    installdepends=("oxide=$pkgver" "oxide-utils=$pkgver" "liboxide=$pkgver" "libsentry=$_sentryver")
    replaces=(fret anxiety)
    conflicts=(fret anxiety)

    package() {
        # Screenshot daemon
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/fret
        install -D -m 644 -t "$pkgdir"/opt/usr/share/applications "$srcdir"/release/opt/usr/share/applications/codes.eeems.fret.oxide
        # Screenshot viewer
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/anxiety
        install -D -m 644 -t "$pkgdir"/opt/usr/share/applications "$srcdir"/release/opt/usr/share/applications/codes.eeems.anxiety.oxide
        install -D -m 644 -t "$pkgdir"/opt/usr/share/icons/oxide/48x48/apps "$srcdir"/release/opt/usr/share/icons/oxide/48x48/apps/image.png
        install -D -m 644 -t "$pkgdir"/opt/usr/share/icons/oxide/702x702/splash "$srcdir"/release/opt/usr/share/icons/oxide/702x702/splash/anxiety.png
    }
}

oxide-utils() {
    pkgdesc="Command line tools for Oxide"
    section="admin"
    installdepends=("oxide=$pkgver" "liboxide=$pkgver" "libsentry=$_sentryver")
    replaces=(rot notify-send update-desktop-database desktop-file-validate)
    conflicts=(rot notify-send update-desktop-database desktop-file-validate)

    package() {
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/rot
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/notify-send
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/update-desktop-database
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/desktop-file-validate
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/xdg-desktop-menu
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/xdg-desktop-icon
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/xdg-open
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/xdg-settings
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/gio
    }
}

inject_evdev() {
    pkgdesc="Inject input events"
    section="utils"
    installdepends=("liboxide=$pkgver" "libsentry=$_sentryver")

    package() {
        install -D -m 755 -t "$pkgdir"/opt/bin "$srcdir"/release/opt/bin/inject_evdev
    }
}

liboxide() {
    pkgdesc="Shared library for oxide applications"
    section="devel"

    package() {
        install -D -m 755 -t "$pkgdir"/opt/lib "$srcdir"/release/opt/lib/liboxide.so*
        install -D -m 755 -t "$pkgdir"/opt/lib "$srcdir"/release/opt/lib/libqsgepaper.so*
    }
}

liboxide-dev() {
    pkgdesc="Shared library for oxide applications"
    section="devel"
    installdepends=("liboxide=$pkgver")

    package() {
        install -D -m 755 -t "$pkgdir"/opt/lib/pkgconfig "$srcdir"/release/opt/lib/pkgconfig/oxide.pc
        install -D -m 755 -t "$pkgdir"/opt/include "$srcdir"/release/opt/include/epframebuffer.h
        install -D -m 755 -t "$pkgdir"/opt/include "$srcdir"/release/opt/include/liboxide.h
        cp -ar "$srcdir"/release/opt/include/liboxide/ "$pkgdir"/opt/include/
    }
}

libsentry() {
    pkgdesc="Sentry SDK for C, C++ and native applications."
    section="devel"
    url=https://github.com/getsentry/sentry-native
    pkgver="$_sentryver"
    timestamp="2024-06-12T08:04:15Z"

    package() {
        install -D -m 755 -t "$pkgdir"/opt/lib "$srcdir"/release/opt/lib/libsentry.so
    }
}
